home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / oasis / oasis1-1.lha / oasis-1.1 / parser.h < prev    next >
C/C++ Source or Header  |  1992-05-01  |  19KB  |  571 lines

  1. /*==========================================================================*
  2.     Oasis Alpha Version 1.1               (C) Copyright 1992 Fah-Chun Cheong
  3.     Revised: 5/1/92 by: fcc@eecs.umich.edu    and The University of Michigan
  4.     ------------------------------------------------------------------------
  5.     Permission to use, copy, modify, distribute, sell and resell Oasis Alpha
  6.     software and its documentation for any purpose and without fee is hereby
  7.     granted, provided that the authorship be appropriately credited and
  8.     acknowledged, and that the above copyright notice appear in all copies
  9.     and both the copyright notice and this permission notice appear in
  10.     supporting documentation. The author makes no representations about the
  11.     suitability of this software for any purpose. It is provided "as is"
  12.     without express or implied warranty. Oasis Alpha is free, caveat emptor!
  13.     ------------------------------------------------------------------------
  14.     To request Oasis Alpha source code:   oasis-alpha-request@eecs.umich.edu
  15.     To enroll in the mailing list:        oasis-alpha-request@eecs.umich.edu
  16.     To send bug reports:                  oasis-alpha-bugs@eecs.umich.edu
  17.     To discuss openly all matters Oasis:  oasis-alpha@eecs.umich.edu
  18.  *==========================================================================*/
  19. #include                <strings.h>
  20. #include                <ctype.h>
  21. #include                <stdio.h>
  22.  
  23. #define LINESZ          1024
  24. #define NUL             0
  25.  
  26. #define HEAD            0x0
  27. #define BODY            0x2
  28. #define MASK            0x3
  29. #define NEXT            0x4
  30. #define LAST            0x7fffffff
  31. #define UNDEF           0xffffffff
  32.  
  33. #define IN              0x0
  34. #define OUT             0x1
  35.  
  36. #define HI              0x0
  37. #define HO              0x1
  38. #define BI              0x2
  39. #define BO              0x3
  40.  
  41. #define FF              0x00
  42. #define FB              0x10
  43. #define FU              0x20
  44. #define BB              0x30
  45.  
  46. #define HI_FF           0x00
  47. #define HI_BB           0x30
  48. #define HO_FF           0x01
  49. #define HO_BB           0x31
  50. #define BI_FF           0x02
  51. #define BI_FB           0x12
  52. #define BI_FU           0x22
  53. #define BI_BB           0x32
  54. #define BO_FF           0x03
  55. #define BO_FB           0x13
  56. #define BO_FU           0x23
  57. #define BO_BB           0x33
  58.  
  59. #define FALSE           0
  60. #define TRUE            1
  61. #define RELOP           2
  62. #define WAIT            5
  63. #define POST            6
  64. #define CALL            7
  65. #define INVK            8
  66. #define SEND            9
  67.  
  68. #define AGENT           11
  69. #define OBJECT          12
  70. #define COND            13
  71. #define LIST            14
  72. #define ARRAY           15
  73. #define DOPE            16
  74. #define ITEM            17
  75. #define HANDLE          18
  76. #define META            19
  77. #define AT              20
  78. #define SITE            21
  79. #define VAR             22
  80. #define REF             23
  81. #define FIELD           24
  82. #define INDEX           25
  83. #define EXPR            26
  84. #define FUN             27
  85. #define ANON            28
  86. #define AS              29
  87. #define IS              30
  88.  
  89. #define TOP             31
  90. #define BOT             32
  91.  
  92. #define ADD             43
  93. #define SUB             45
  94. #define MUL             42
  95. #define DIV             47
  96. #define REM             37
  97.  
  98. #define RADD            -ADD
  99. #define RSUB            -SUB
  100. #define RMUL            -MUL
  101. #define RDIV            -DIV
  102. #define RREM            -REM
  103.  
  104. #define LLIST           -LIST
  105. #define LDOPE           -DOPE
  106. #define LVAR            -VAR
  107. #define LREF            -REF
  108.  
  109. #define Aoff(atts)      (atts  ? atts->aoff  + 1 : 4)
  110. #define Soff(specs)     (specs ? specs->soff + 1 : 1)
  111. #define Coff(conds)     (conds ? conds->coff + 1 : 2)
  112. #define Moff(mets)      (mets  ? mets->moff  + 1 : 1)
  113. #define Voff(node)      (((Var *) node->this.p)->voff)
  114.  
  115. #define diff(s1,s2)     ( strcmp(s1, s2))
  116. #define same(s1,s2)     (!strcmp(s1, s2))
  117. #define seen(l,l2,p,p2) (l == l2 || l > l2 && p >= p2)
  118.  
  119. #define mode(s,f)       ((s | f) & MASK)
  120. #define null(t)         (t == Int  ? Izero\
  121.                         :t == Real ? Rzero\
  122.                         :t == Char ? Blank : Nil)
  123.  
  124. #define isin(s,f)       (mode(s,f) == BI || mode(s,f) == HO)
  125. #define isout(s,f)      (mode(s,f) == BO || mode(s,f) == HI)
  126. #define isident(c)      (isalnum(c) || c == '_')
  127. #define islast(rs,mo)   (!rs->next || rs->next->head->moff != mo)
  128. #define islive(vs,s)    (vs->def < s && s < vs->use)
  129.  
  130. #define isclass(t)      (t->tag == AGENT || t->tag == OBJECT || t->tag == COND)
  131. #define ispointer(t)    (t->tag == AGENT || t->tag == OBJECT || t->tag == ARRAY || t->tag == LIST)
  132. #define isword(t)       (t->tag == INT   || t->tag == REAL   || t->tag == CHAR)
  133. #define ismeta(t)       (t->tag == META)
  134.  
  135. #define flip(tag)       tag *= -1
  136. #define Malloc(x)       (x *) malloc(sizeof(x))
  137.  
  138. #define err0(s)         point_error(sprintf(errbuf, s))
  139. #define err1(s,a)       point_error(sprintf(errbuf, s, a))
  140. #define err2(s,a,b)     point_error(sprintf(errbuf, s, a, b))
  141. #define err3(s,a,b,c)   point_error(sprintf(errbuf, s, a, b, c))
  142.  
  143. #define search_spec(id,ss,l,sz,b,gs,cs,ks,as,ms) if (!lookup_spec(id,ss,l,sz,b,gs,cs,ks,as,ms))\
  144.                                                                           err1("Unspecified class `%s'", id)
  145. #define search_base(id,bs,l,ms)       if (!lookup_base(id,bs,l,ms))       err1("Unspecified base `%s'", id)
  146. #define search_gene(id,gs,go)         if (!lookup_gene(id,gs,go))         err1("Unspecified generic `%s'", id)
  147. #define search_cond(id,cs,co)         if (!lookup_cond(id,cs,co))         err1("Unspecified condition `%s'", id)
  148. #define search_att(id,l,p,as,ao,t)    if (!lookup_att(id,l,p,as,ao,t))    err1("Unspecified attribute `%s'", id)
  149. #define search_met(id,l,p,ms,mo,q,as) if (!lookup_met(id,l,p,ms,mo,q,as)) err1("Unspecified method `%s'", id)
  150.  
  151. #define clash_spec(id,ss)   if (find_spec(id,ss))                   err1("Class `%s' already specified", id)
  152. #define clash_gene(id,gs)   if (find_gene(id,gs))                   err1("Generic `%s' already specified", id)
  153. #define clash_cond(id,cs)   if (find_cond(id,cs))                   err1("Condition `%s' already specified", id)
  154. #define clash_cst(id,ks)    if (find_cst(id,ks))                    err1("Constant `%s' already specified", id)
  155. #define clash_att(id,l,as)  if (find_att(id,l,PROTECTED,as))        err1("Attribute `%s' already specified", id)
  156. #define clash_met(id,l,ms)  if (find_met(id,l,PROTECTED,ms))        err1("Method `%s' already specified", id)
  157. #define clash_arg(id,as)    if (find_arg(id,as))                    err1("Argument `%s' already specified", id)
  158. #define clash_imp(id,is)    if (find_imp(id,is))                    err1("Class `%s' already implemented", id)
  159. #define clash_head(id,rs,r) if (find_head(id,rs,r))                 err1("Method `%s' already implemented", id)
  160.  
  161. #define count_genes(gs)     if (gs)                                 err0("Missing generics")
  162. #define count_pars(as)      if (as)                                 err0("Missing method parameters")
  163. #define count_props(as)     if (as)                                 err0("Missing class properties")
  164. #define count_indices(ds)   if (ds)                                 err0("Missing array indices")
  165. #define count_sizes(ds)     if (ds)                                 err0("Missing array sizes")
  166. #define count_items(sz)     if (sz > 0)                             err0("Missing components in array")
  167. #define count_dims(ds)      if (ds && ds->next)                     err0("Missing dimensions in array")
  168.  
  169. #define count_gene(gs,l,id) if (!gs && l > 0)                       err1("Extra generic `%s'", id)
  170. #define count_par(as)       if (!as)                                err0("Extra parameter in method")
  171. #define count_prop(as)      if (!as)                                err0("Extra property in class instance")
  172. #define count_index(ds)     if (!ds)                                err0("Extra index in array")
  173. #define count_size(ds)      if (!ds)                                err0("Extra size in array")
  174. #define count_item(sz)      if (sz == 0)                            err0("Extra component in array")
  175. #define count_dim(ds)       if (ds && !ds->next)                    err0("Extra dimension in array")
  176.  
  177. #define check_gene(gs,l,id) if (gs && l > 0 && diff(id, gs->name))  err2("Bad generic `%s', expect `%s'", id, gs->name)
  178. #define check_agent(k)      if (k   != AGENT)                       err0("Goal clause invalid for object class")
  179. #define check_remote(tag)   if (tag != SEND)                        err0("Concurrent message is to agents only")
  180. #define check_lvalue(tag)   if (tag != REF && tag != VAR)           err0("Constant does not have lvalue")
  181. #define check_self(n)       if (n == Self)                          err0("Undefined self out of context")
  182.  
  183. #define get_name(t)         (isclass(t)       ? t->u.class->name  :                                          "")
  184. #define get_cons(t)         (isclass(t)       ? t->u.class->cons  :                                          NUL)
  185. #define get_item(t)         (t->tag == ARRAY  ? t->u.array->item  :                                          Top)
  186. #define get_dims(t)         (t->tag == ARRAY  ? t->u.array->dims  : (Dim  *) (err0("Unexpected array"),     NUL))
  187. #define get_elem(t)         (t->tag == LIST   ? t->u.list         : (Type *) (err0("Unexpected list"),      Top))
  188. #define get_object(t)       (t->tag == OBJECT ? t->u.class->name  : (char *) (err0("Unexpected object"),     ""))
  189. #define get_agent(t)        (t->tag == AGENT  ? t->u.class->name  : (char *) (err0("Unexpected agent"),      ""))
  190.  
  191. #define ref_class(t)        (isclass(t)       ? t->u.class->name  : (char *) (err0("Invalid destination"),   ""))
  192. #define ref_object(t)       (t->tag == OBJECT ? t->u.class->name  : (char *) (err0("Bad object reference"),  ""))
  193. #define ref_dims(t)         (t->tag == ARRAY  ? t->u.array->dims  : (Dim *)  (err0("Bad array reference"),  NUL))
  194.  
  195. typedef struct _File    File;
  196. typedef struct _Spec    Spec;
  197. typedef struct _Gene    Gene;
  198. typedef struct _Cond    Cond;
  199. typedef struct _Cst     Cst;
  200. typedef struct _Att     Att;
  201. typedef struct _Met     Met;
  202. typedef struct _Arg     Arg;
  203. typedef struct _Type    Type;
  204. typedef struct _Class   Class;
  205. typedef struct _Con     Con;
  206. typedef struct _Array   Array;
  207. typedef struct _Dim     Dim;
  208. typedef struct _Imp     Imp;
  209. typedef struct _Rule    Rule;
  210. typedef struct _Head    Head;
  211. typedef struct _Clu     Clu;
  212. typedef struct _Pred    Pred;
  213. typedef struct _Var     Var;
  214. typedef struct _Par     Par;
  215. typedef struct _Prop    Prop;
  216. typedef struct _Node    Node;
  217. typedef struct _Goal    Goal;
  218.  
  219. struct _File {
  220.         char    name[1024];
  221.         int     line;
  222.         char   *buf;
  223.         char   *ptr;
  224. };
  225.  
  226. struct _Spec {
  227.         char   *name;
  228.         int     soff;
  229.         int     level;
  230.         int     size;
  231.         Spec   *base;
  232.         Gene   *genes;
  233.         Cond   *conds;
  234.         Cst    *csts;
  235.         Att    *atts;
  236.         Met    *mets;
  237.         int     mark;
  238.         Spec   *next;
  239. };
  240.  
  241. struct _Gene {
  242.         char   *name;
  243.         int     goff;
  244.         Gene   *next;
  245. };
  246.  
  247. struct _Cond {
  248.         char   *name;
  249.         int     coff;
  250.         Cond   *next;
  251. };
  252.  
  253. struct _Cst {
  254.         char   *name;
  255.         Node   *node;
  256.         Cst    *next;
  257. };
  258.  
  259. struct _Att {
  260.         char   *name;
  261.         int     aoff;
  262.         int     level;
  263.         int     prot;
  264.         Type   *type;
  265.         Node   *init;
  266.         Att    *next;
  267. };
  268.  
  269. struct _Met {
  270.         char   *name;
  271.         int     moff;
  272.         int     level;
  273.         int     prot;
  274.         Arg    *args;
  275.         Met    *next;
  276. };
  277.  
  278. struct _Arg {
  279.         char   *name;
  280.         int     flow;
  281.         Type   *type;
  282.         Arg    *next;
  283. };
  284.  
  285. struct _Type {
  286.         int     tag;
  287.         union {
  288.          Array *array;
  289.          Type  *list;
  290.          Class *class;
  291.          int    meta;
  292.         }       u;
  293. };
  294.  
  295. struct _Class {
  296.         char   *name;
  297.         Con    *cons;
  298. };
  299.  
  300. struct _Con {
  301.         Type   *type;
  302.         Con    *next;
  303. };
  304.  
  305. struct _Array {
  306.         Type   *item;
  307.         Dim    *dims;
  308. };
  309.  
  310. struct _Dim {
  311.         int     size;
  312.         Dim    *next;
  313. };
  314.  
  315. struct _Imp {
  316.         char   *name;
  317.         Spec   *spec;
  318.         Goal   *goal;
  319.         Rule   *rules;
  320.         Imp    *next;
  321. };
  322.  
  323. struct _Rule {
  324.         Var    *vars;
  325.         Head   *head;
  326.         Clu    *body;
  327.         Clu    *tail;
  328.         Pred   *last;
  329.         Rule   *next;
  330. };
  331.  
  332. struct _Head {
  333.         char   *name;
  334.         int     moff;
  335.         int     prot;
  336.         Par    *pars;
  337. };
  338.  
  339. struct _Clu {
  340.         Pred   *pred;
  341.         int     step;
  342.         Clu    *next;
  343. };
  344.  
  345. struct _Pred {
  346.         int     tag;
  347.         int     moff;
  348.         Par    *pars;
  349.         Node   *dest;
  350.         Pred   *next;
  351. };
  352.  
  353. struct _Var {
  354.         char   *name;
  355.         int     voff;
  356.         int     state;
  357.         int     def;
  358.         int     use;
  359.         Type   *hout;
  360.         Type   *bint;
  361.         Type   *mint;
  362.         Var    *next;
  363. };
  364.  
  365. struct _Par {
  366.         int     flow;
  367.         Node   *node;
  368.         Par    *next;
  369. };
  370.  
  371. struct _Prop {
  372.         Node   *node;
  373.         Prop   *next;
  374. };
  375.  
  376. struct _Node {
  377.         int     tag;
  378.         Type   *type;
  379.         union {
  380.          Node  *p;
  381.          int    i;
  382.          double f;
  383.         }       this;
  384.         Node   *that;
  385. };
  386.  
  387. struct _Goal {
  388.         Att    *atts;
  389.         Var    *vars;
  390.         Clu    *body;
  391. };
  392.  
  393. extern  Type           *Top;
  394. extern  Type           *Bot;
  395. extern  Type           *Int;
  396. extern  Type           *Real;
  397. extern  Type           *Char;
  398. extern  Type           *Condv;
  399. extern  Node           *Izero;
  400. extern  Node           *Rzero;
  401. extern  Node           *Blank;
  402. extern  Node           *Nil;
  403. extern  Node           *Anon;
  404. extern  Node           *Self;
  405. extern  Node           *Site;
  406.  
  407. extern  Spec           *specs;
  408. extern  Imp            *imps;
  409. extern  Goal           *goal;
  410. extern  Att            *Atts0;
  411.  
  412. extern  char           *fname;
  413. extern  int             lineno;
  414. extern  int             errono;
  415. extern  char            errbuf[];
  416.  
  417. extern  int             atoi();
  418. extern  long            atol();
  419. extern  double          atof();
  420.  
  421. extern  Spec           *Spec_();
  422. extern  Gene           *Gene_();
  423. extern  Cond           *Cond_();
  424. extern  Cst            *Cst_();
  425. extern  Att            *Att_();
  426. extern  Met            *Met_();
  427. extern  Arg            *Arg_();
  428. extern  Type           *Type_();
  429. extern  Class          *Class_();
  430. extern  Con            *Con_();
  431. extern  Array          *Array_();
  432. extern  Dim            *Dim_();
  433. extern  Imp            *Imp_();
  434. extern  Rule           *Rule_();
  435. extern  Head           *Head_();
  436. extern  Clu            *Clu_();
  437. extern  Pred           *Pred_();
  438. extern  Var            *Var_();
  439. extern  Par            *Par_();
  440. extern  Prop           *Prop_();
  441. extern  Node           *Node_p();
  442. extern  Node           *Node_i();
  443. extern  Node           *Node_f();
  444. extern  Goal           *Goal_();
  445.  
  446. extern  Spec           *find_spec();
  447. extern  Spec           *find_base();
  448. extern  Gene           *find_gene();
  449. extern  Cond           *find_cond();
  450. extern  Cst            *find_cst();
  451. extern  Att            *find_att();
  452. extern  Met            *find_met();
  453. extern  Arg            *find_arg();
  454. extern  Imp            *find_imp();
  455. extern  Head           *find_head();
  456. extern  Var            *find_var();
  457. extern  Spec           *lookup_spec();
  458. extern  Spec           *lookup_base();
  459. extern  Gene           *lookup_gene();
  460. extern  Cond           *lookup_cond();
  461. extern  Att            *lookup_att();
  462. extern  Met            *lookup_met();
  463.  
  464. extern  Att            *subs_ratts();
  465. extern  Arg            *subs_args();
  466. extern  Type           *subs_type();
  467. extern  Con            *subs_cons();
  468. extern  Type           *subs_meta();
  469. extern  Con            *make_cons();
  470. extern  Att            *next_atts();
  471.  
  472. extern  int             grab_size();
  473. extern  void            update_vars();
  474. extern  void            undo_vars();
  475. extern  void            mark_var();
  476. extern  void            check_bound();
  477. extern  void            check_vars();
  478. extern  void            check_lvar();
  479. extern  void            check_var();
  480. extern  void            check_lref();
  481. extern  void            check_ref();
  482. extern  void            check_relop();
  483. extern  void            check_num();
  484. extern  void            check_nil();
  485. extern  void            check_index();
  486. extern  void            check_size();
  487. extern  void            check_dim();
  488. extern  void            check_string();
  489. extern  void            check_handle();
  490. extern  void            check_name();
  491. extern  Type           *sub_type();
  492. extern  Type           *min_type();
  493. extern  int             match_type();
  494. extern  int             match_name();
  495. extern  int             match_cons();
  496. extern  int             match_dims();
  497. extern  int             match_meta();
  498.  
  499. extern  Pred           *eval_relop();
  500. extern  Node           *eval_arop();
  501. extern  Node           *eval_uminus();
  502. extern  Node           *eval_fun();
  503. extern  Node           *eval_float();
  504. extern  int             eval_cost();
  505. extern  Pred           *apply_relop_c();
  506. extern  Pred           *apply_relop_i();
  507. extern  Pred           *apply_relop_f();
  508. extern  Node           *apply_arop_i();
  509. extern  Node           *apply_arop_f();
  510. extern  Node           *apply_fun_i();
  511. extern  Node           *apply_fun_f();
  512.  
  513. extern  int             len_props();
  514. extern  int             len_pars();
  515. extern  Par            *next_pars();
  516. extern  int             match_pars();
  517. extern  int             max_vars();
  518. extern  int             in_node();
  519. extern  int             in_pars();
  520. extern  int             in_preds();
  521. extern  int             in_clus();
  522. extern  int             in_rules();
  523. extern  void            print_node();
  524. extern  void            print_tag();
  525. extern  void            print_tags();
  526. extern  void            print_type();
  527. extern  void            print_types();
  528. extern  void            print_dims();
  529. extern  void            print_props();
  530. extern  int             print_rpars();
  531. extern  void            print_pars();
  532. extern  int             print_rargs();
  533. extern  void            print_vars();
  534. extern  void            print_last();
  535. extern  int             print_preds();
  536. extern  int             print_clus();
  537. extern  void            print_rules();
  538. extern  void            print_atts();
  539. extern  void            print_goal();
  540. extern  void            print_spec();
  541. extern  void            print_specs();
  542. extern  void            print_imps();
  543. extern  void            print_classes();
  544. extern  char           *print_globals();
  545. extern  char           *print_locals();
  546. extern  void            print_main();
  547.  
  548. extern  void            point_error();
  549. extern  void            line_error();
  550. extern  void            track_line();
  551. extern  void            lex_escape();
  552. extern  int             lex_token();
  553. extern  void            xxerror();
  554. extern  int             xxlex();
  555. extern  void            init_lex();
  556.  
  557. extern  int             xxparse();
  558. extern  void            init_par();
  559. extern  void            parse();
  560.  
  561. extern  int             read_file();
  562. extern  int             write_file();
  563. extern  int             new_include();
  564. extern  char           *include();
  565. extern  void            filter();
  566. extern  int             pprocess();
  567.  
  568. extern  void            compile();
  569. extern  void            main();
  570.  
  571.